package com.martin.lib_base.ui.screen

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.PagerState
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.unit.dp
import androidx.lifecycle.lifecycleScope
import com.martin.lib_base.helper.HeartBeatHelper
import com.martin.lib_base.ktx.getLifecycleOwner
import dev.DevUtils
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch

/**
 * @author：孟凡华
 * @date：2023/6/29
 * @desc：
 */

private const val MAX_PAGE_COUNT = 1000

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun CommonBanner(
    // 数据源
    data: List<Any>,
    // 是否循环
    isLoop: Boolean = true,
    // 是否自动播放
    isAutoPlay: Boolean = true,
    // 自动播放间隔
    interval: Long = 3000,
    // 是否显示指示器
    showIndicator: Boolean = true,
    // 页面状态
    pagerState: PagerState = rememberPagerState(initialPage = if (isLoop) getLoopStartIndex(data.size) else 0) { if (isLoop) MAX_PAGE_COUNT else data.size },
    // 修饰符
    modifier: Modifier = Modifier,
    item: @Composable (Any) -> Unit
) {
    val coroutineScope = rememberCoroutineScope()

    Box(modifier = modifier) {
        HorizontalPager(
            modifier = Modifier.fillMaxSize(),
            state = pagerState
        ) {
            if (data.isNotEmpty()) {
                item(data[it % data.size])
            }
        }
        if (showIndicator) {
            Box(
                modifier = Modifier
                    .align(Alignment.BottomCenter)
                    .padding(bottom = 7.5.dp)
            ) {
                PointIndicator(pageCount = data.size, pagerState = pagerState)
            }
        }
    }

    // 自动播放
    val scope = LocalLifecycleOwner.current.lifecycleScope
    LaunchedEffect(Unit) {
        if (isAutoPlay) {
            flow {
                for (i in 0..Int.MAX_VALUE) {
                    delay(interval)
                    emit(i)
                }
            }
                .onEach {
                    coroutineScope.launch {
                        pagerState.animateScrollToPage(pagerState.currentPage + 1)
                    }
                }
                .launchIn(scope)
        }
    }
}

/**
 * 获取循环开始的索引
 * @param size 数据源大小
 */
fun getLoopStartIndex(size: Int): Int {
    val middle = MAX_PAGE_COUNT / 2
    if (size <= 0) return middle
    return middle - middle % size
}
